﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wjtour.Domain;
using Wjtour.BLL;
using Hjp.FrameWork.Common;
using Wjtour.Common;

namespace Wjtour.Web.Admin.article
{
    public partial class list : BasePage
    {
        #region 固定分页参数
        private int page = 1;
        private int pagesize = 20;
        #endregion

        article_bll _bll = new article_bll();
        protected PaginationData<Wjtour.Domain.dto.article_articletype> data = new PaginationData<Domain.dto.article_articletype>();
        protected TextBox q;
        protected DropDownList cid;
        protected Button submit;
        articletype_bll _tbll = new articletype_bll();
        protected HiddenField delIds;
        protected Button delButton;
        protected string type;
        protected Button auditButton;
        protected DropDownList auditStatus;

        protected void Page_Load(object sender, EventArgs e)
        {
            page = Utils.StrToInt(Request["page"], 1);
            type = Request["type"];
            submit.Click += new EventHandler(submit_Click);
            delButton.Click += new EventHandler(delButton_Click);
            auditButton.Click += auditButton_Click;
            bindList();
            if (!IsPostBack)
            {
                BindDropdown();
                q.Text = Request["q"];
            }
        }

        void auditButton_Click(object sender, EventArgs e)
        {
            int isaudit = Utils.StrToInt(auditStatus.SelectedValue, 1);
            bool b = _bll.BatchAudit(delIds.Value, isaudit) > 0;

            if (b)
            {
                SaveLog("Autid");
                Utils.Alert(this.Page, "审核成功！");
                Utils.Redirect(this.Page, "list.aspx?type=" + type + "&q=" + q.Text, false);
            }
        }

        void delButton_Click(object sender, EventArgs e)
        {
            bool b = _bll.BatchDelete(delIds.Value) > 0;

            if (b)
            {
                SaveLog("Delete");
                Utils.Alert(this.Page, "删除成功！");
                Utils.Redirect(this.Page, "list.aspx?type=" + type + "&q=" + q.Text, false);
            }
        }

        void BindDropdown()
        {
            string sql = "select * from articletype";
            if (!string.IsNullOrEmpty(type))
                sql = "select * from articletype where id in (" + type + ")";

            List<articletype> types = _tbll.GetAll<articletype>(sql).ToList();
            if (types != null && types.Count > 0)
            {
                List<articletype> ptypes = types.Where(t => t.pid == 0).ToList();
                foreach (var ptype in ptypes)
                {
                    cid.Items.Add(new ListItem(ptype.title, ptype.id.ToString()));
                    List<articletype> subtypes = types.Where(t => t.pid == ptype.id).ToList();
                    if (subtypes != null && subtypes.Count > 0)
                    {
                        foreach (var item in subtypes)
                        {
                            cid.Items.Add(new ListItem(" ┝ " + item.title, item.id.ToString()));
                        }
                    }
                }
            }
        }

        void submit_Click(object sender, EventArgs e)
        {
            bindList();
        }

        void bindList()
        {
            string t = cid.SelectedValue.ToString() == "0" ? type : cid.SelectedValue.ToString();
            data = _bll.GetList(q.Text, t, page - 1, pagesize);
            PagerHtml = Utility.pageft(data.Pager.TotalRecords, "/admin/article/list.aspx?type=" + t + "&q=" + HttpUtility.UrlEncode(q.Text), pagesize);
        }
    }
}